﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment2
{
    public class Clock
    {
        private DateTime curTime;
        private DateTime alarmTime;
        private bool isRunning;

        public event Action Tick;
        public event Action<string> Alarm;

        public void SetAlarmTime(DateTime time)
        {
            alarmTime = time;
            Console.WriteLine($"闹钟时间已设置为: {alarmTime.ToString("HH:mm:ss")}");
        }
        public void Start()
        {
            isRunning = true;
            curTime = DateTime.Now;

            while (isRunning)
            {
                curTime = DateTime.Now;

                Tick?.Invoke();
                if (curTime >= alarmTime)
                {
                    Alarm?.Invoke("闹钟响铃啦！");
                    isRunning = false;
                }

                Thread.Sleep(1000);
            };
        }
    }
}
